package com.jcnet.bizaia.tools;

import org.apache.http.Header;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jcnet.bizaia.App;
import com.jcnet.bizaia.R;
import com.jcnet.bizaia.adapter.FavouriteDiscussAdapter;
import com.jcnet.bizaia.adapter.FavouriteVodAdapter;
import com.jcnet.bizaia.adapter.FavouriteVoiceAdapter;
import com.jcnet.bizaia.bean.StateBean;
import com.jcnet.bizaia.view.ProgressHUD;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class JCFacouriteTools {

	private int didNum;
	
	private int ftypeNum;
	
	private Context context;
	
	private AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
	
	private ProgressHUD progressHUD;
	
	public void delFavourite(final Context context, int didNum, int ftypeNum){
		
		this.context = context;
		this.didNum = didNum;
		this.ftypeNum  = ftypeNum;
		
		/**
		 * useridNum	int	用户编号
			didNum	int	收藏对象ID
			ftypeNum	int	收藏类型（0：讨论会1：动画2：音频）
		 */
		RequestParams params = new RequestParams();
		params.put("useridNum", JCTools.getUserBean(context));
		params.put("didNum", this.didNum+"");
		params.put("ftypeNum", this.ftypeNum+"");
		
		asyncHttpClient.post(App.HTTP_PATH+"favo_delelteFavourites.action?", params, new AsyncHttpResponseHandler(){
			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				progressHUD = ProgressHUD.show(context, context.getString(R.string.commiting), true	, true, null);
				super.onStart();
			}
			
			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				// TODO Auto-generated method stub
				super.onSuccess(arg0, arg1, arg2);
				
				StateBean stateBean = new Gson().fromJson(new String(arg2), new TypeToken<StateBean>(){}.getType());
				
				if(stateBean.getState()==200){
					
					Toast.makeText(JCFacouriteTools.this.context, context.getString(R.string.delSuccess), 0).show();
					
					if(JCFacouriteTools.this.ftypeNum==2){
						JCFacouriteTools.this.context.sendBroadcast(new Intent(FavouriteVoiceAdapter.DELVOICE_SUCCESS));
					}
					if(JCFacouriteTools.this.ftypeNum==0){
						
						JCFacouriteTools.this.context.sendBroadcast(new Intent(FavouriteDiscussAdapter.DELDISCUSS_SUCCESS));
					}
					if(JCFacouriteTools.this.ftypeNum==1){
						JCFacouriteTools.this.context.sendBroadcast(new Intent(FavouriteVodAdapter.DELVOD_SUCCESS));
					}
					
				}else{
					Toast.makeText(JCFacouriteTools.this.context, stateBean.getMessage()+"", 0).show();
				}
			}
			
			@Override
			public void onFinish() {
				// TODO Auto-generated method stub
				super.onFinish();
				if(progressHUD.isShowing()){
					progressHUD.cancel();
				}
			}
			
			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				// TODO Auto-generated method stub
				super.onFailure(arg0, arg1, arg2, arg3);
				if(progressHUD.isShowing()){
					progressHUD.cancel();
				}
				Toast.makeText(JCFacouriteTools.this.context, JCFacouriteTools.this.context.getText(R.string.serverError), 0).show();
			}
		});
		
	}
}
